/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim</a>, 2005
 */
package hk.com.carfield.task;

import hk.com.carfield.core.Context;
import hk.com.carfield.util.LangUtil;
import hk.com.carfield.util.UrlUtil;
import hk.com.carfield.web.Request;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public final class Statist implements Executor {
	private final Map<String, AtomicInteger> fileCountMap = new ConcurrentHashMap<String, AtomicInteger>();

	public int runPerDay(final Context c) {
		final int count = fileCountMap.size();
		c.mailMan.handleMap("directory click count", fileCountMap);
		c.mailMan.handleMap("rename count", UrlUtil.renameMap);
		UrlUtil.renameMap.clear();
		fileCountMap.clear();
		return count;
	}

	public int runPerRequest(final Context c, final Request r, final File l)
			throws UnsupportedEncodingException {
		// TODO trace: no. , file size (bandwidth), ip, referurl
		LangUtil.incrementMap(
				fileCountMap,
				UrlUtil.getRelativePath(c.config,
						l.isDirectory() ? l : l.getParentFile()));
		return 1;
	}

	public int runPerWeek(final Context c) {
		return 0;
	}
}
